package com.hua.yuan.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.hua.yuan.BaseFragment;
import com.hua.yuan.R;
import com.hua.yuan.base.Constents;
import com.hua.yuan.base.ITabFragment;
import com.hua.yuan.provider.user.IUserProvider;
import com.hua.yuan.provider.user.User;
import com.hua.yuan.ui.FavoriteActivity;
import com.hua.yuan.ui.LoginActivity;
import com.hua.yuan.ui.MoneyBagActivity;
import com.hua.yuan.ui.RegisterActivity;
import com.hua.yuan.ui.UserInfoActivity;
import com.hua.yuan.userlogin.UserLoginModule;
import com.hua.yuan.util.GlideCircleTransform;
import com.hua.yuan.util.ToastUtil;

import cm.hua.yuan.common.utils.AppContextManager;
import cm.hua.yuan.common.utils.PreferenceUtils;

/**
 * Created by huangyang on 2018/5/8.
 * 我的fragment
 */

public class MeFragment extends BaseFragment implements View.OnClickListener, ITabFragment {

    private RelativeLayout meLoginRl;//登录界面
    private TextView meFgRegister;//注册
    private ImageView me_user_img;//显示用户的头像

    private LinearLayout meFg_ll1;
    private LinearLayout meFg_ll2;
    private LinearLayout meFg_ll3;
    private LinearLayout meFg_ll4;
    private LinearLayout meFg_ll5;
    private LinearLayout meFg_ll6;
    private LinearLayout meFg_ll7;
    private LinearLayout meFg_ll8;
    private LinearLayout meFg_ll9;

    private TextView clNum;
    private TextView msgNum;

    private RelativeLayout clearCacheRl;
    private LinearLayout clearLl;
    private TextView showCache;
    private TextView cacheQd;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_me, null);
        initView(view);

        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        IUserProvider provider = UserLoginModule.getInstance().getProvider();
        if (provider == null) {
            displayShowRegister();
            return;
        }
        //如果登录成功
        if (!provider.getIsLogin()) {
            displayShowRegister();
            return;
        }

        User userinfo = provider.getUser();
        if (userinfo == null) {
            displayShowRegister();
            return;
        }
        String userHeaderUrl = userinfo.getHeadUrl();
        if (TextUtils.isEmpty(userHeaderUrl)) {
            Glide.with(context).load(R.drawable.default_img)
                    .asBitmap()
                    .transform(new GlideCircleTransform(context))
                    .into(me_user_img);
        } else {

            Glide.with(context).load(userHeaderUrl)
                    .asBitmap()
                    .transform(new GlideCircleTransform(context))
                    .into(me_user_img);
        }
        meFgRegister.setText(provider.getMobilePhone());
        me_user_img.setVisibility(View.VISIBLE);
        me_user_img.setOnClickListener(this);
        clNum.setText("345");
        msgNum.setText("10");
    }


    private void displayShowRegister() {
        meFgRegister.setText("点击注册");
        me_user_img.setVisibility(View.GONE);
        clNum.setText("一");
        msgNum.setText("");
    }

    private void initView(View view) {

        meLoginRl = view.findViewById(R.id.meLoginRl);
        meFgRegister = view.findViewById(R.id.meFgRegister);
        meFg_ll1 = view.findViewById(R.id.meFg_ll1);
        meFg_ll2 = view.findViewById(R.id.meFg_ll2);
        meFg_ll3 = view.findViewById(R.id.meFg_ll3);
        meFg_ll4 = view.findViewById(R.id.meFg_ll4);
        meFg_ll5 = view.findViewById(R.id.meFg_ll5);
        meFg_ll6 = view.findViewById(R.id.meFg_ll6);
        meFg_ll7 = view.findViewById(R.id.meFg_ll7);
        meFg_ll8 = view.findViewById(R.id.meFg_ll8);
        meFg_ll9 = view.findViewById(R.id.meFg_ll9);
        me_user_img = view.findViewById(R.id.me_user_img);

        clNum = view.findViewById(R.id.clNum);
        msgNum = view.findViewById(R.id.msgNum);

        clearCacheRl = view.findViewById(R.id.clearCacheRl);
        clearLl = view.findViewById(R.id.clearLl);
        showCache = view.findViewById(R.id.showCache);
        cacheQd = view.findViewById(R.id.cacheQd);

    }


    private void initData() {

        meLoginRl.setOnClickListener(this);
        meFgRegister.setOnClickListener(this);
        meFg_ll1.setOnClickListener(this);
        meFg_ll2.setOnClickListener(this);
        meFg_ll3.setOnClickListener(this);
        meFg_ll4.setOnClickListener(this);
        meFg_ll5.setOnClickListener(this);
        meFg_ll6.setOnClickListener(this);
        meFg_ll7.setOnClickListener(this);
        meFg_ll8.setOnClickListener(this);
        meFg_ll9.setOnClickListener(this);
        clearCacheRl.setOnClickListener(this);
        cacheQd.setOnClickListener(this);
        clearLl.setOnClickListener(this);
        me_user_img.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.meLoginRl:
                //登录界面
                startActivity(new Intent(context, LoginActivity.class));
                break;
            case R.id.meFgRegister:
                //注册界面
                boolean isLogin = PreferenceUtils.getBoolean(Constents.IS_LOGIN, false);

                if (!isLogin) {
                    startActivity(new Intent(context, RegisterActivity.class));
                }

                break;
            case R.id.meFg_ll1:
                //我的钱包
                if (isLogin()) {
                    Intent intent = new Intent(context, MoneyBagActivity.class);
                    intent.putExtra("buyMoney", "-1");
                    intent.putExtra("movieId", "-1");
                    startActivity(intent);
                }

                break;
            case R.id.meFg_ll2:

                ToastUtil.showShortToast("开发中，敬请期待!");
                break;
            case R.id.meFg_ll3:
                ToastUtil.showShortToast("开发中，敬请期待!");
                break;
            case R.id.meFg_ll4:
                startActivity(new Intent(context, FavoriteActivity.class));
                break;
            case R.id.meFg_ll5:
                ToastUtil.showShortToast("开发中，敬请期待!");
                break;
            case R.id.meFg_ll6:
                ToastUtil.showShortToast("开发中，敬请期待!");
                break;
            case R.id.meFg_ll7:
                ToastUtil.showShortToast("开发中，敬请期待!");
                break;
            case R.id.meFg_ll8:
                clearCacheRl.setVisibility(View.VISIBLE);
                break;
            case R.id.meFg_ll9:
                PreferenceUtils.setBoolean(Constents.IS_LOGIN, false);
                meFgRegister.setText("点击注册");
                me_user_img.setVisibility(View.GONE);
                break;
            case R.id.clearCacheRl:
                clearCacheRl.setVisibility(View.GONE);
                break;
            case R.id.cacheQd:
                clearCacheRl.setVisibility(View.GONE);
                break;
            case R.id.me_user_img:
                startActivity(new Intent(context, UserInfoActivity.class));
                break;
        }

    }


    public boolean isLogin() {
        boolean flag = PreferenceUtils.getBoolean(Constents.IS_LOGIN);

        if (!flag) {
            //没有登录 先登录
            startActivity(new Intent(getActivity(), LoginActivity.class));
        }


        return flag;

    }

    @Override
    public String getTabName() {
        return AppContextManager.getInstance().getContext().getString(R.string.my);
    }

    @Override
    public int getDrawableId() {
        return R.mipmap.main_me_unselect;
    }

    @Override
    public int getDrawableSelectId() {
        return R.mipmap.main_me_select;
    }

    @Override
    public Fragment getTabFragment() {
        return this;
    }
}
